//- Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. 
//- <https://www.chatopera.com>, Licensed under the Chunsong Public 
//- License, Version 1.0  (the "License"), https://docs.cskefu.com/licenses/v1.html
//- Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
//- Copyright (C) 2018-Jun. 2023 Chatopera Inc, <https://www.chatopera.com>
//- Licensed under the Apache License, Version 2.0
//- http://www.apache.org/licenses/LICENSE-2.0
script(language='javascript').
    var data ;
    var config = {} ;

if inviteData
    script(language='javascript')
        include fingerprint.js

    script(language='javascript').
        var cskefuOnlineUserId;
        var ukefu = {
            time: new Date().getTime(),
            chat: "/im/index.html?appid=#{appid}!{id?'&id='+id:''}!{aiid?'&aiid='+aiid:''}!{exchange?'&exchange='+exchange:''}!{name?'&name='+name:''}!{email?'&email='+email:''}!{phone?'&phone='+phone:''}&client=#{client}&type=text!{skill?'&skill='+skill:''}!{agent?'&agent='+agent:''}",
            config: function (d) {
                data = d;
                if (d.id != null && d.id != '') {
                    cskefuOnlineUserId = d.id;
                }
            },
            openChatDialog: function () {
                var url = ukefu.chat;
                if (data) {
                    if (data.id) {
                        url = url + "&id=" + data.id;
                    }
                    if (data.name) {
                        url = url + "&name=" + encodeURIComponent(data.name);
                    }
                    if (data.email) {
                        url = url + "&email=" + encodeURIComponent(data.email);
                    }
                    if (data.phone) {
                        url = url + "&phone=" + encodeURIComponent(data.phone);
                    }
                }
                window.location.replace(url + "&t=" + new Date().getTime());
            }
        }
        Fingerprint2.get({extraComponents: [!{ channelVisitorSeparate == true?'{key: "cskefuAppId", getData: function(done, options){done("'+appid+'")}},':''}
        {
            key: "rndId",
                getData(done, options)
            {
                var rnd = '';
                if (localStorage) {
                    rnd = localStorage.finger;
                    if (!rnd) {
                        rnd = Math.random();
                        localStorage.finger = rnd;
                    }
                }

                done(rnd);
            }
        }
        ]}, function (components) {
            var glue = components.map(function (component) {
                return component.value
            })
            cskefuOnlineUserId = Fingerprint2.x64hash128(glue.join(''), 31)
            var userid = '#{userid}';
            ukefu.chat = ukefu.chat + "&userid="+ (userid || cskefuOnlineUserId) + "&sessionid=#{sessionid}!{ai?'&ai='+ai:''}!{title?'&title='+title:''}!{traceid?'&url='+url:''}!{traceid?'&traceid='+traceid:''}";
            ukefu.openChatDialog();
        });
        